package org.gl.training.gwt.client.view;

import org.gl.training.gwt.client.service.TaskService;
import org.gl.training.gwt.client.service.TaskServiceAsync;
import org.gl.training.gwt.client.utils.MyAsyncCallback;
import org.gl.training.gwt.shared.model.TaskVO;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class TaskView extends Window {

	private TextItem eventItem;

	private TextItem timeItem;

	private TextItem estimateDurationItem;

	private TextItem actualDurationItem;

	private TextItem commentsItem;

	private DynamicForm form;

	private TaskVO taskVO;

	private DailyPlanView dailyPlanView;

	private TaskServiceAsync taskService = GWT.create(TaskService.class);

	public TaskView(TaskVO taskVO, DailyPlanView dailyPlanView) {
		super();

		this.taskVO = taskVO;
		this.dailyPlanView = dailyPlanView;

		setMembersMargin(15);
		setWidth(350);
		setHeight(250);
		setTitle("Task");
		setShowMinimizeButton(false);
		setShowCloseButton(false);
		setIsModal(true);
		setShowModalMask(true);
		centerInPage();

		VLayout layout = new VLayout(10);

		form = new DynamicForm();
		form.setErrorOrientation(FormErrorOrientation.RIGHT);
		form.setNumCols(3);
		form.setColWidths("54%", "5%", "41%");
		form.setLayoutAlign(Alignment.LEFT);

		eventItem = new TextItem();
		eventItem.setTitle("Event*");
		eventItem.setRequired(true);

		timeItem = new TextItem();
		timeItem.setTitle("Time*");
		timeItem.setRequired(true);

		estimateDurationItem = new TextItem();
		estimateDurationItem.setTitle("Estimate duration*");
		estimateDurationItem.setRequired(true);

		actualDurationItem = new TextItem();
		actualDurationItem.setTitle("Actual duration");

		commentsItem = new TextItem();
		commentsItem.setTitle("Comments");

		form.setFields(new FormItem[] { eventItem, timeItem,
				estimateDurationItem, actualDurationItem, commentsItem });

		HLayout hLayout = new HLayout(10);

		Button SaveButton = new Button("Save");
		SaveButton.addClickHandler(new SaveHandler());

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new CancelHandler());

		hLayout.addMember(SaveButton);
		hLayout.addMember(cancelButton);
		hLayout.setAlign(Alignment.CENTER);

		layout.addMember(form);
		layout.addMember(hLayout);

		addItem(layout);

		updateView();
	}

	private void updateView() {
		eventItem.setValue(taskVO.getEvent());
		timeItem.setValue(taskVO.getTime());
		estimateDurationItem.setValue(taskVO.getEstimateDuration());
		actualDurationItem.setValue(taskVO.getActualDuration());
		commentsItem.setValue(taskVO.getComments());
	}

	private void updateTaskVO() {
		String event = eventItem.getValueAsString();
		String time = timeItem.getValueAsString();
		String estimateDuration = estimateDurationItem.getValueAsString();
		String actualDuration = actualDurationItem.getValueAsString();
		String comments = commentsItem.getValueAsString();

		taskVO.setEvent(event);
		taskVO.setTime(time);
		taskVO.setEstimateDuration(estimateDuration);
		taskVO.setActualDuration(actualDuration);
		taskVO.setComments(comments);
	}

	private class TaskSavedCallback extends MyAsyncCallback<Void> {

		@Override
		public void onSuccess(Void result) {
			hide();
			dailyPlanView.loadDailyPlan();
		}
	}

	private class SaveHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				updateTaskVO();
				taskService.saveTask(taskVO, new TaskSavedCallback());
			}
		}
	}

	private class CancelHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			hide();
		}
	}
}
